package com.hxh.utils;

/**
 * @author: Hxh
 * @date: 2019/11/21
 * @description: 和事务相关的工具类，它包含了，开启事务，提交事务，回滚事务和释放连接
 */
public class TransactionManager {
    private  ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    //开启事务
    public void beginTransction(){
        try {


        connectionUtils.getThreadConnection().setAutoCommit(false);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //提交事务
    public void commit(){
        try {


            connectionUtils.getThreadConnection().commit();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    //回滚事务
    public void rollback(){
        try {


            connectionUtils.getThreadConnection().rollback();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //释放连接
    public void release(){
        try {

            connectionUtils.getThreadConnection().close();
            //把连接还回连接池中   线程还回线程池中
            connectionUtils.removeConnection();
            //把线程与连接解绑
        }catch (Exception e){
            e.printStackTrace();
        }
    }


}
